<template>
  <div :ref="ref" class="decoration-wrap">
    <svg :width="width" :height="height">
      <polyline
        :stroke="colors[1]"
        stroke-width="2"
        :points="`0, ${height / 2} ${width}, ${height / 2}`"
      />

      <polyline
        :stroke="colors[0]"
        stroke-width="2"
        :points="`5, ${height / 2} ${width * 0.2 - 3}, ${height / 2}`"
        :stroke-dasharray="`0, ${width * 0.2}`"
        fill="freeze"
      >
        <animate
          :id="animationId2"
          attributeName="stroke-dasharray"
          :values="`0, ${width * 0.2};${width * 0.2}, 0;`"
          dur="3s"
          :begin="`${animationId1}.end`"
          fill="freeze"
        />
        <animate
          attributeName="stroke-dasharray"
          :values="`${width * 0.2}, 0;0, ${width * 0.2}`"
          dur="0.01s"
          :begin="`${animationId7}.end`"
          fill="freeze"
        />
      </polyline>

      <polyline
        :stroke="colors[0]"
        stroke-width="2"
        :points="`${width * 0.2 + 3}, ${height / 2} ${width * 0.8 - 3}, ${height / 2}`"
        :stroke-dasharray="`0, ${width * 0.6}`"
      >
        <animate
          :id="animationId4"
          attributeName="stroke-dasharray"
          :values="`0, ${width * 0.6};${width * 0.6}, 0`"
          dur="3s"
          :begin="`${animationId3}.end + 1s`"
          fill="freeze"
        />
        <animate
          attributeName="stroke-dasharray"
          :values="`${width * 0.6}, 0;0, ${width * 0.6}`"
          dur="0.01s"
          :begin="`${animationId7}.end`"
          fill="freeze"
        />
      </polyline>

      <polyline
        :stroke="colors[0]"
        stroke-width="2"
        :points="`${width * 0.8 + 3}, ${height / 2} ${width - 5}, ${height / 2}`"
        :stroke-dasharray="`0, ${width * 0.2}`"
      >
        <animate
          :id="animationId6"
          attributeName="stroke-dasharray"
          :values="`0, ${width * 0.2};${width * 0.2}, 0`"
          dur="3s"
          :begin="`${animationId5}.end + 1s`"
          fill="freeze"
        />
        <animate
          attributeName="stroke-dasharray"
          :values="`${width * 0.2}, 0;0, ${width * 0.3}`"
          dur="0.01s"
          :begin="`${animationId7}.end`"
          fill="freeze"
        />
      </polyline>

      <circle cx="2" :cy="height / 2" r="2" :fill="colors[1]">
        <animate
          :id="animationId1"
          attributeName="fill"
          :values="`${colors[1]};${colors[0]}`"
          :begin="`0s;${animationId7}.end`"
          dur="0.3s"
          fill="freeze"
        />
      </circle>

      <circle :cx="width * 0.2" :cy="height / 2" r="2" :fill="colors[1]">
        <animate
          :id="animationId3"
          attributeName="fill"
          :values="`${colors[1]};${colors[0]}`"
          :begin="`${animationId2}.end`"
          dur="0.3s"
          fill="freeze"
        />
        <animate
          attributeName="fill"
          :values="`${colors[1]};${colors[1]}`"
          dur="0.01s"
          :begin="`${animationId7}.end`"
          fill="freeze"
        />
      </circle>

      <circle :cx="width * 0.8" :cy="height / 2" r="2" :fill="colors[1]">
        <animate
          :id="animationId5"
          attributeName="fill"
          :values="`${colors[1]};${colors[0]}`"
          :begin="`${animationId4}.end`"
          dur="0.3s"
          fill="freeze"
        />
        <animate
          attributeName="fill"
          :values="`${colors[1]};${colors[1]}`"
          dur="0.01s"
          :begin="`${animationId7}.end`"
          fill="freeze"
        />
      </circle>

      <circle :cx="width - 2" :cy="height / 2" r="2" :fill="colors[1]">
        <animate
          :id="animationId7"
          attributeName="fill"
          :values="`${colors[1]};${colors[0]}`"
          :begin="`${animationId6}.end`"
          dur="0.3s"
          fill="freeze"
        />
        <animate
          attributeName="fill"
          :values="`${colors[1]};${colors[1]}`"
          dur="0.01s"
          :begin="`${animationId7}.end`"
          fill="freeze"
        />
      </circle>
    </svg>
  </div>
</template>

<script>
import { uuid } from '../../border/util'
export default {
  name: 'HzxDecoration',
  props: {
    colors: {
      type: Array,
      default: () => (['#00c2ff', 'rgba(0, 194, 255, 0.3)'])
    },
    width: {
      type: Number,
      default: 200
    },
    height: {
      type: Number,
      default: 100
    }
  },
  data() {
    const id = uuid()
    return {
      ref: 'decoration-10',

      animationId1: `d10ani1${id}`,
      animationId2: `d10ani2${id}`,
      animationId3: `d10ani3${id}`,
      animationId4: `d10ani4${id}`,
      animationId5: `d10ani5${id}`,
      animationId6: `d10ani6${id}`,
      animationId7: `d10ani7${id}`,

      defaultColor: ['#00c2ff', 'rgba(0, 194, 255, 0.3)']

    }
  },
  mounted() {
    // const { calcSVGData } = this
    this.calcSVGData()
  },
  methods: {
    afterAutoResizeMixinInit() {
      // const { calcSVGData } = this
      this.calcSVGData()
    },
    calcSVGData() {
      const { calcPointsPosition, calcRectsPosition, calcScale } = this
      calcPointsPosition()
      calcRectsPosition()
      calcScale()
    },
    calcPointsPosition() {
      // const { svgWH, rowNum, rowPoints } = this
      const [w, h] = this.svgWH
      const horizontalGap = w / (this.rowPoints + 1)
      const verticalGap = h / (this.rowNum + 1)
      const points = new Array(this.rowNum).fill(0).map((foo, i) =>
        new Array(this.rowPoints).fill(0).map((foo, j) => [
          horizontalGap * (j + 1), verticalGap * (i + 1)
        ]))
      this.points = points.reduce((all, item) => [...all, ...item], [])
      console.log(this.points)
    },
    calcRectsPosition() {
      // const { points, rowPoints } = this
      const rect1 = this.points[this.rowPoints * 2 - 1]
      const rect2 = this.points[this.rowPoints * 2 - 3]
      this.rects = [rect1, rect2]
    },
    calcScale() {
      // const { width, height, svgWH } = this
      const [w, h] = this.svgWH
      this.svgScale = [this.width / w, this.height / h]
    },
    onResize() {
      const { calcSVGData } = this
      calcSVGData()
    }
  }
}
</script>
<style lang="less" scoped="scoped">
    .decoration-wrap {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
        svg {
          transform-origin: left top;
        }
    }
</style>
